package com.lagou.controller;

import com.lagou.bean.UpLoadResult;
import com.lagou.service.FileUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/pic")
public class UploadController {
    @Autowired
    private FileUpLoadService  fileUpLoadService;
    @PostMapping("/upload")
    @ResponseBody
    public UpLoadResult upload(@RequestParam("file") MultipartFile  multipartFile){
        return  fileUpLoadService.upload(multipartFile);
    }

    @GetMapping("/download")
    @ResponseBody
    public void download(String uid, HttpServletResponse response){
        UpLoadResult upLoadResult = new UpLoadResult();
        try {
            fileUpLoadService.downloadFile(uid,response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public UpLoadResult download(String uid){
        UpLoadResult upLoadResult = new UpLoadResult();
        fileUpLoadService.deleteFile(uid);
        upLoadResult.setUid(uid);
        upLoadResult.setStatus("success");
        return upLoadResult;
    }
}
